import Vue from 'vue'

/*
 * 时间戳 --> yyyy-mm-dd hh:mm:ss
 * */
Vue.filter('formatDateTime', dateTime => {
  if (!dateTime) return
  let date = new Date(Number(dateTime) * 1000)
  let y = date.getFullYear()
  let m = date.getMonth() + 1
  m = m < 10 ? ('0' + m) : m
  let d = date.getDate()
  d = d < 10 ? ('0' + d) : d
  let h = date.getHours()
  h = h < 10 ? ('0' + h) : h
  let minute = date.getMinutes()
  minute = minute < 10 ? ('0' + minute) : minute
// let second = date.getSeconds()
// second = second < 10 ? ('0' + second) : second
  return y + '-' + m + '-' + d + ' ' + h + ':' + minute
})

/*
 * 时间戳 --> yyyy-mm-dd
 * */
Vue.filter('formatDate', (dateTime, str) => {
  if (!dateTime) return
  if (dateTime.toString().indexOf('-') > 0) return dateTime
  let date = new Date(Number(dateTime) * 1000)
  let y = date.getFullYear()
  let m = date.getMonth() + 1
  m = m < 10 ? ('0' + m) : m
  let d = date.getDate()
  d = d < 10 ? ('0' + d) : d
  return y + (str||'-') + m + (str||'-') + d
})